<!DOCTYPE html>
<html>

<head lang="en">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>最家-注册</title>
  <link rel="stylesheet" type="text/css" href="css/public.css" />
  <link rel="stylesheet" type="text/css" href="css/login.css" />
</head>

<body>
  <!-------------------reg-------------------------->
  <div class="reg">
    <form action="#" method="post">
      <h1><a href="index.html"><img src="img/temp/logo.png"></a></h1>
      <p><h2>用户注册</h2></p>
      <p><input type="text" name="" value="" placeholder="请输入邮箱"></p>
      <p><input type="password" name="" value="" placeholder="请输入密码"></p>
      <!-- <p><input type="text" name="" value="" placeholder="请确认密码"></p>
      <p class="txtL txt"><input class="code" type="text" name="" value="" placeholder="验证码">
        <img src="img/temp/code.jpg">
      </p> -->
      <p><input type="button" name="" value="注册"></p>
      <p class="txtL txt">完成此注册，即表明您同意了我们的<br>
        <a href="javascript:;"><使用条款和隐私策略></a>
      </p>
      <p class="txt"><a href="login.html"><span></span>已有账号登录</a></p>
    </form>
  </div>
</body>
<script>
  //1 获取页面中的节点
  let inputObj = document.querySelectorAll('input');
  //2 给登录绑定事件 DOM二级
  inputObj[2].addEventListener('click', ajaxFn);

  function ajaxFn() {
    //3 获取表单数据
    let name = inputObj[0].value;
    let pwd = inputObj[1].value;
    //4 调用ajax发送数据
    let xhr = new XMLHttpRequest();
    xhr.open('post', './data/reg.php');
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    let data = `user=${name}&pwd=${pwd}`;
    xhr.send(data);
    console.log(data);
    //5 接收结果进行提示
    xhr.onreadystatechange = function () {
      if (xhr.readyState == 4 && xhr.status == 200) {
        let str = xhr.responseText;
        str = JSON.parse(str);
        console.log(str);
        // 6 根据返回的结果进行提示
        if (str.code == 200){
          alert("注册成功,点击进入登录页面");
          window.location.href='login.html';
        } 
        if (str.code == 500) alert("注册失败，用户名或密码输入错误，请重新尝试！");
      }
    }
  }
</script>
</html>